﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace Hangu.WebClient
{
    public class MailMessage
    {
       
        private string _SenderAddress = HelperCommon.SenderAddress;
        public string SenderAddress
        {
            get { return _SenderAddress; }
            set { _SenderAddress = value; }
        }
        private string _SenderName = HelperCommon.SenderName;
        public string SenderName
        {
            get { return _SenderName; }
            set { _SenderName = value; }
        }
        private string _SenderPwd = HelperCommon.SenderPwd;
        /// <summary>
        /// 发件人邮箱密码
        /// </summary>
        public  string SenderPwd { get { return _SenderPwd; } set { _SenderPwd = value; } }

        private string _SMTPService = HelperCommon.SMTPService;
        /// <summary>
        /// SMTP服务地址
        /// </summary>
        public  string SMTPService { get { return _SMTPService; } set { _SMTPService = value; } }

        public string MailSubject { get; set; }
        public string MailBody { get; set; }
        public string ErrorMsg { get; set; }
        /// <summary>
        /// 接收邮件地址
        /// </summary>
        public List<string> EmailTo { get; set; }
        public List<string> EmailCC { get; set; }
        /// <summary>
        /// 附件
        /// </summary>
        public List<Attachment> Attachments { get; set; }

        public MailMessage()
        {
            EmailTo = EmailTo ?? new List<string>();
            EmailCC = EmailCC ?? new List<string>();
            Attachments = Attachments ?? new List<Attachment>();
        }
        /// <summary>
        /// 通过LocalHost发邮件
        /// </summary>
        public bool SendMailLocalhost()
        {
            bool tag = false;
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            foreach (var item in EmailTo)
            { msg.To.Add(item); }
            foreach (var item in EmailCC)
            { msg.CC.Add(item); }
            foreach (var item in Attachments)
            {
                msg.Attachments.Add(item);
            }
            msg.From = new MailAddress(SenderAddress, SenderName, System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址（可以随便写），发件人姓名，编码*/
            msg.Subject = MailSubject;//邮件标题
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
            msg.Body = MailBody;//邮件内容
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
            msg.IsBodyHtml = false;//是否是HTML邮件
            msg.Priority = MailPriority.High;//邮件优先
           
            SmtpClient client = new SmtpClient();
            client.Host = "localhost";
            object userState = msg;
            try//简单一点儿可以
            {
                client.Send(msg);
                tag = true;
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                ErrorMsg = ex.Message;
            }
            return tag;
        }
        /// <summary>
        /// 通过SMPT发邮件
        /// </summary>
        public bool SendMailSMTP()
        {
            bool tag = false;
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            foreach (var item in EmailTo)
            { msg.To.Add(item); }
            foreach (var item in EmailCC)
            { msg.CC.Add(item); }
            foreach (var item in Attachments)
            {
                msg.Attachments.Add(item);
            }
            msg.From = new MailAddress(SenderAddress);// new MailAddress(SenderAddress, SenderName, System.Text.Encoding.UTF8);
            /* 上面3个参数分别是发件人地址（可以随便写），发件人姓名，编码*/
            msg.Subject = MailSubject;//邮件标题
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
            msg.Body = MailBody;//邮件内容
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
            msg.IsBodyHtml = false;//是否是HTML邮件
            msg.Priority = MailPriority.High;//邮件优先
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential(this.SenderName, this.SenderPwd);//注册的邮箱和密码
            client.Host = SMTPService;
            object userState = msg;
            try
            {
                client.Send(msg);
                tag = true;
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                ErrorMsg = ex.Message;
            }
            return tag;
        }
    }
}
